# coding=utf-8
from docx import Document
from docx.shared import Cm


def replace_picture(doc, tag, pic, width=15):
    for paragraph in doc.paragraphs:
        if tag in paragraph.text:
            has_replaced = False
            for run in paragraph.runs:
                run.clear()
                if not has_replaced:
                    run.add_picture(pic, width=Cm(width))
                    has_replaced = True


if __name__ == '__main__':
    document = Document(r"E:\日常工作\数据组日常工作-韩冲\生成每日必办\[内部] 每日必办 8.17.docx")
    replace_picture(document, "#{qztp}1", r"E:\日常工作\数据组日常工作-韩冲\生成每日必办\图1. 七日诉求量情况.png")
    fileMustDoSave1 = r'E:\日常工作\数据组日常工作-韩冲\生成每日必办\[内部] 每日必办-1.docx'
    document.save(fileMustDoSave1)
